package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Users;
import com.jr.service.UsersService;
import com.jr.service.impl.IUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@WebServlet("/userlogin/*")
public class UserController extends HttpServlet {
    UsersService us = new IUserServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println("我是servicecontroller"+"sssssssssssssssssssssssssssssss");
    }

    public void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*通过get请求传过来的参数 进行 sql查询 与数据库进行匹配*/
        String account = request.getParameter("account");
        String password = request.getParameter("password");
       // System.out.println("*****"+account+"*******"+password);
        Users u = new Users();
        u.setAccount(account);
        u.setPassword(password);
        /*通过对象拿信息*/
        //System.out.println(u.getAccount()+":"+u.getPassword());
        boolean b = us.login(u);
        System.out.println(u.getPhone());
        response.getWriter().println(new Gson().toJson(b));
    }
    /*要根据输入的姓名和身份证号 判断这个人是否存在在用户表中 如果存在在用户表中 那就继续填写后面的内容
    * 点击提交按钮的时候 */
    public void queryUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*根据输入的账号找出他的电话号 显示出来 然后用户输入姓名和身份证 隐形比对 与该账号所属对象的姓名和身份证号进行对比*/
        String account  = request.getParameter("account");
        System.out.println("拿account值 拿到了吗？"+account);//有值

        Users users = us.queryByAccount(account);
        System.out.println(users+"count拿对象");
        System.out.println("当前对象的身份证号：："+users.getCardnum());

        System.out.println(users.getPhone()+"通过对象账号拿对象手机号 ");
        response.getWriter().println(new Gson().toJson(users));
    }

}
